package com.sjy.project.ent.api;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.ent.data.domain.EntData;
import com.sjy.project.ent.data.service.IEntDataService;
import com.sjy.project.ent.yearSta.domain.EntKpiYearSta;
import com.sjy.project.system.dept.domain.Dept;
import com.sjy.project.system.dept.service.IDeptService;
import com.sjy.project.system.user.domain.User;
import com.sjy.project.system.user.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 企业档案Controller
 * 
 * @author ljb
 * @date 2022-06-04
 */
@RestController
@Api(value = "亩均档案管理接口相关", tags = {"亩均接口"})
@RequestMapping("/api/ent/data")
public class ApiEntDataController extends BaseController
{

    @Autowired
    private IEntDataService entDataService;

    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;
	 /**
     * 新增保存企业档案信息
     */
    @ApiOperation(value = "101、新增企业", position = 101, notes = "")
    @ApiImplicitParams({
    	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
    	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
    	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
    	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
    	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
    	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
    	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
    	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
    	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
    	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
    	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
    	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
    	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
    	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
    	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
    	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
    	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
    	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
    	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
    	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
    	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
    	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
	 })
    @PostMapping("/add")
    public ResponseEntity<BaseResponse> addSave(@ApiIgnore EntData entData)
    {
    	//检测是否有重名CreditCode
    	EntData exist = entDataService.selectEntDataByCreditCode(entData.getCreditCode(),entData.getDataYear());
    	if(exist!=null) {
    		return BaseResponse.generateBadResponseEntity("新增失败-当前名称已存在", entData);
    	}

    	SecuryUserDetail user = UserContext.getUserSession();
    	entData.setCreatedBy(user.getLoginName());
    	entData.setImportType(2);
    	
    	int flag = entDataService.insertEntData(entData);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("新增成功"); 
    	}
    	return BaseResponse.generateBadResponseEntity("新增失败", entData);
    }
    
    /**
     * 删除企业档案信息
     */
    @Log(title = "企业档案信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "102、删除企业档案信息", position = 102, notes = "英文半角逗号分割")
    @ApiImplicitParams({
	  @ApiImplicitParam(name = "ids", value = "id ,拼接字符串", required = false, example = "海城"),
		 
	 })
    @PostMapping( "/remove")
    public ResponseEntity<BaseResponse<String>> remove(String ids)
    {
    	int flag = entDataService.deleteEntDataByIds(ids);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("删除成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("删除失败", ids);
    }
    
    /**
     * 修改保存企业档案信息
     */
    @Log(title = "企业档案信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "103、修改企业档案信息", position = 103, notes = "")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "编号", required = false, example = ""),
    	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
    	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
    	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
    	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
    	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
    	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
    	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
    	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
    	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
    	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
    	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
    	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
    	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
    	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
    	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
    	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
    	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
    	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
    	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
    	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
    	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
    	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
    	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
    	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
		 
	 })
    public ResponseEntity<BaseResponse> editSave(@ApiIgnore EntData entData)
    {
    	if(entData.getId()==null) {
        	return BaseResponse.generateBadResponseEntity("修改的时候Id不能为空", null);
    	}
    	SecuryUserDetail user = UserContext.getUserSession();
    	entData.setUpdateBy(user.getLoginName());
    	int flag = entDataService.updateEntData(entData);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("修改成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("修改失败", null);
    }

    
    @ApiOperation(value = "104、获取企业档案清单", position = 104, notes = "当前页:pageNum,分页大小：pageSize,排序字段：orderByColumn，排序方式：asc")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "编号", required = false, example = ""),
  	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
  	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
  	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
  	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
  	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
  	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
  	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
  	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
  	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
  	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
  	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
  	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
  	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
  	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
  	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
  	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
  	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
  	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
  	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
  	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
  	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
  	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
  	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
  	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
  	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
    })
    @PostMapping({"/list"})
    public ResponseEntity<BaseResponse<TableDataInfo<EntData>>> list(@ApiIgnore EntData entData)
    {
    	SecuryUserDetail tokenUser = UserContext.getUserSession();
    	User user = userService.selectUserById(tokenUser.getUserId());
    	List<Dept> depts = deptService.selectStreetDepts(user.getDeptId());
    	if(depts==null||depts.size()==0) {
    		return BaseResponse.generateOKResponseEntity(getDataTable(null));
    	}
    	if(depts.size()==1) {
    		entData.setTown(depts.get(0).getDeptName());
    	}
        startPage();
        List<EntData> list = entDataService.selectEntDataList(entData);
        return BaseResponse.generateOKResponseEntity(getDataTable(list));
    }
    
    @ApiOperation(value = "105、根据id获取场所信息详情", position = 105, notes = "")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "id", value = "编号", required = false, example = "1")
    })
    @PostMapping({"/getById"})
    public ResponseEntity<BaseResponse<EntData>> getById(Long id) throws Exception
    { 
    	EntData entData = entDataService.selectEntDataById(id);
    	if(entData != null) {
    		return BaseResponse.generateOKResponseEntity(entData);
    	}
    	return BaseResponse.generateBadResponseEntity(500,"获取企业信息失败", entData);
    	
    }
    /**
     * 导入数据
     */
    @ApiOperation(value = "106、导入企业信息", position = 106, notes = "")
    @Log(title = "企业档案信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "file", value = "文件", required = false, example = "",dataType = "file"),
	  @ApiImplicitParam(name = "updateSupport", value = "是否更新", required = false, example = ""),
    })
    public ResponseEntity<BaseResponse<Object>> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EntData> util = new ExcelUtil<EntData>(EntData.class);
        List<EntData> list = util.importExcel(file.getInputStream());
        String message = entDataService.importEnt(list, updateSupport);
        return BaseResponse.generateOKResponseEntity(message);
    }
    
    @RequiresPermissions("fym:place:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<EntData> util = new ExcelUtil<EntData>(EntData.class);
        return util.importTemplateExcel("企业档案信息数据");
    }
    /**
     * 导出企业档案信息列表
     */
    @ApiOperation(value = "107、导出企业档案信息模板", position = 107, notes = "")
    @Log(title = "企业档案信息", businessType = BusinessType.EXPORT)
    @RequestMapping(value="/export", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "编号", required = false, example = ""),
	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
  })
    public ResponseEntity<BaseResponse<Object>> export(@ApiIgnore EntData entData,HttpServletResponse response)
    {
        List<EntData> list = entDataService.selectEntDataList(entData);
        ExcelUtil<EntData> util = new ExcelUtil<EntData>(EntData.class);
        //util.exportExcel(list, "企业档案信息数据");
        util.exportExcel(response,list, "企业档案信息数据");
        return null;
    }
    
    
    @Log(title = "街道确认企业基础信息", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmByStreet")
    @ApiOperation(value = "108、街道确认企业基础信息准确性", position = 108, notes = "企业基础数据确认完就不能修改了")

    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "id", value = "编号", required = false, example = "1"),
  	  @ApiImplicitParam(name = "confirmFlag", value = "确认状态 1是0否", required = false, example = "1")
    })
    public ResponseEntity<BaseResponse> confirmByStreet(Long id,Integer confirmFlag)
    {
    	EntData data = entDataService.selectEntDataById(id);
    	if(data == null) {
    		return BaseResponse.generateBadResponseEntity("id无效", null);
    	}
    	SecuryUserDetail user1 = UserContext.getUserSession();
    	User user = userService.selectUserById(user1.getUserId());
    	if(!"00".equals(user.getUserType())||!user.getDept().getDeptName().equals(data.getTown())) {
    		return BaseResponse.generateBadResponseEntity("无权修改-当前账号非企业对应街道账号", null);
    	}
    	data.setConfirmFlag(confirmFlag);
    	data.setConfirmTime(new Date());
    	
    	if(entDataService.updateEntData(data)>0) {
            return BaseResponse.generateOKResponseEntity("修改成功"); 
    	}else {
    		return BaseResponse.generateBadResponseEntity("修改失败", null);
    	}
    }

}
